import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: "" || sessionStorage.getItem("token"),
    username: "" || sessionStorage.getItem("username"),
  },
  mutations: {
    // 保存用户登陆token
    saveUserToken(state, token) {
      sessionStorage.setItem("token", token);
      state.token = token;
    },
    saveUserName(state, username) {
      sessionStorage.setItem("username", username);
      state.username = username;
    },
  },
  actions: {
    saveUserToken({ commit }, token) {
      commit("saveUserToken", token);
    },
    saveUserName({ commit }, username) {
      commit("saveUserName", username);
    },
  },
  getters: {
    username: (state) => state.username,
  },
  modules: {},
});
